package org.delphy.indy2.containers
{
	import caurina.transitions.Tweener;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import mx.containers.HBox;
	import mx.core.UIComponent;

	public class CarbonTabDock extends HBox
	{
		
		public var transitionTime:Number=1;
		
		private var resizing:Boolean=false;
		
		private var contentByWidget:Dictionary;
		private var statusByWidget:Dictionary;
		
		private static const EXPAND_ACTION:String="expand";
		private static const SHRINK_ACTION:String="shrink";
		
		public function CarbonTabDock()
		{
			super();
			
			styleName="carbonTabDock";
			
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
			
			contentByWidget=new Dictionary();
			statusByWidget=new Dictionary();
		}
		
		public function addWidget(obj:Object):void{
			// The widget to be added to the dock is of type "Object". It should contain two entries:
			// obj:{widget:UIComponent,content:UIComponent}
			
			var widget:UIComponent=obj.widget;
			var content:UIComponent=obj.content;
			
			content.width=0;
			
			widget.addEventListener(MouseEvent.ROLL_OVER,onWidgetRollOver);
			widget.addEventListener(MouseEvent.ROLL_OUT,onWidgetRollOut);
			widget.addEventListener(MouseEvent.CLICK,onWidgetClick);
			
			addChild(widget);
			addChild(content);
			
			contentByWidget[widget]=content;
			statusByWidget[widget]=false;
		} 
		
		private function onWidgetRollOver(evt:MouseEvent):void{
			var widget:UIComponent=evt.currentTarget as UIComponent;
			if(!statusByWidget[widget])
				expandDockWithTarget(widget);
		}
		
		private function onWidgetRollOut(evt:MouseEvent):void{
			var widget:UIComponent=evt.currentTarget as UIComponent;
			if(!statusByWidget[widget])
				shrinkDockWithTarget(widget);

		}
		
		private function onWidgetClick(evt:MouseEvent):void{
			pinWidget(evt.currentTarget as UIComponent);
		}
		
		private function expandDockWithTarget(target:UIComponent):void{
			var content:UIComponent=contentByWidget[target];
			var contentFullWidth:Number=content.measuredWidth;
			
			// If necessary, resize the dock
			resizeDock(content,EXPAND_ACTION);
			
			Tweener.addTween(content,{width:contentFullWidth,time:transitionTime});
		}
		
		private function shrinkDockWithTarget(target:UIComponent):void{
			var content:UIComponent=contentByWidget[target];
			var contentFullWidth:Number=content.measuredWidth;
			
			// If necessary, resize the dock
			resizeDock(content,SHRINK_ACTION);
			
			Tweener.addTween(content,{width:0,time:transitionTime});
			
		}
		
		private function resizeDock(currentContent:UIComponent=null,currentAction:String=null):void{
			var expectedWidth:Number=0;
			
			for each(var child:UIComponent in this.getChildren()){
				if(child==currentContent){
					if(currentAction==EXPAND_ACTION)
						expectedWidth+=child.measuredWidth;
					else if(currentAction==SHRINK_ACTION)
						expectedWidth+=0;
				}
				else{
					expectedWidth+=child.width;
				}
				
				expectedWidth+=getStyle("horizontalGap");
			}
						
			Tweener.addTween(this,{width:expectedWidth,time:transitionTime,onComplete:onDockResizeComplete});
			resizing=true;
		}
		
		private function onDockResizeComplete(evt:Event=null):void{
			resizing=false;
		}
		
		private function pinWidget(target:UIComponent):void{
			statusByWidget[target]=!statusByWidget[target];
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}	
	}
}